Skip to content

Latest commit

 

History

History
52 lines (46 loc) · 927 Bytes

File metadata and controls

52 lines (46 loc) · 927 Bytes

231. Power of Two

Given an integer, write a function to determine if it is a power of two.

Example 1:

Input: 1 Output: true Explanation: 20 = 1 

Example 2:

Input: 16 Output: true Explanation: 24 = 16 

Example 3:

Input: 218 Output: false 

Solutions (Rust)

1. n & (n - 1)

implSolution{pubfnis_power_of_two(n:i32) -> bool{if n <= 0{false}else{ n &(n - 1) == 0}}}

2. n / 2

implSolution{pubfnis_power_of_two(n:i32) -> bool{if n <= 0{false}elseif n == 1{true}else{ n % 2 == 0 && Self::is_power_of_two(n / 2)}}}
close